<template>
  <el-dialog :visible="showDialog" :title="`${showText}权限点`" @close="btnCancel">
  <el-form ref="formData" :rules="rules" :model="formData" label-width="120px">
    <el-form-item prop="name" label="权限名称">
      <el-input  v-model="formData.name"/>
    </el-form-item>
    <el-form-item prop="code" label="权限标识">
      <el-input v-model="formData.code"/>
    </el-form-item>
    <el-form-item label="权限描述" >
      <el-input v-model="formData.description"/>
    </el-form-item>
    <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
  </el-form>
  <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
  </el-dialog>
</template>

<script>
import {addPermission,updatePermission,getPermissionDetail} from '@/api/permission'
export default{
  props:{
    showDialog:{
      type:Boolean,
      default:false
    },
    pidd:{
      type:Number,
      default:null
    },
    typee:{
      type:Number,
      default:null
    },
    currentNodeId:{
      type:Number,
      default:null
    }
   
  },
  data() {
      return {
        
        formData:{
          name:'',  //权限点名称
          description:'', //权限点描述
          // type:type,  // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
          code:'',  //权限点标识
          // pid:pid,  // 因为做的是树 需要知道添加到哪个节点下了
          enVisible:'0',  // 默认关闭
          pid:'',
          type:''
          
        },
        rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      },
      }
    },
    computed: {
    showText() {
      return this.currentNodeId ? '编辑' : '新增'
    }
  },
    created() {
      
    },
    methods:{
      btnOK() {
        this.$refs.formData.validate(async (isOK)=>{
            if(isOK) {
             if (this.currentNodeId) {
              
              await updatePermission({...this.formData})
              this.$message.success('编辑权限点成功')
              
             }else{
              await addPermission({...this.formData,pid:this.pidd,type:this.typee})  
              
              
              this.$message.success('添加权限点成功')
            }
            this.$emit('updateDepartment')
            this.btnCancel()
            }
        })
      },
      btnCancel() {
        this.formData={
          name:'',  //权限点名称
          description:'', //权限点描述
          type:'',  // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
          code:'',  //权限点标识
          pid:'',  // 因为做的是树 需要知道添加到哪个节点下了
          enVisible:'0',  // 默认关闭
          
        }
        this.$emit('updateCurrentNodeId', null);
        
        this.$refs.formData.resetFields()
        this.$emit('update:showDialog', false)
      },
      async getPermissionDetail() {
        this.formData =await getPermissionDetail(this.currentNodeId)
    }
    }
}
</script>

<style>

</style>